// pages/live.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    deviceSerial:[],
    channelNo:[],
    selIndex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

     if(options.id!=undefined){
        this.setData({
          id:options.id
        });
        this.monitorView();
      }
      
    /*var url = decodeURIComponent(options.url);     
      this.setData({
        live_url:url
      });*/
  },

  //组装地址
  liveurl:function(){
      var link = this.data.link;
      var accessToken = this.data.accessToken;
      var selIndex = this.data.selIndex;
      var deviceSerial = this.data.deviceSerial;
      var channelNo = this.data.channelNo;
      var id = this.data.id;
      var live_url = link+deviceSerial[selIndex]+"/"+channelNo[selIndex]+".live&accessToken="+accessToken+"&id="+id;
      this.setData({
        live_url:live_url
      });

  },

  //获取直播地址
  monitorView:function(){
    var id = this.data.id;
    var params = {id:id};
    var that = this;
    app.request('post', '/api/Project/monitorView', params).then(res => {
      let deviceSerial = res.data.deviceSerial;
      let channelNo = res.data.channelNo;
      let accessToken = res.data.accessToken;
      let link = res.data.link;
      that.setData({
        deviceSerial:deviceSerial,channelNo:channelNo,accessToken:accessToken,link:link
      });
      that.liveurl();
  }).catch(err => console.log(err));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})